<template>
	<view>
		<!-- 顶部导航 -->
		<u-navbar :background="background">
			<view class="ft_16 fw-b">
				收货地址
			</view>
		</u-navbar>
		<view class="content">
			<radio-group v-model="value1">					
				<view class="con_1" v-for="(item,index) in addressList" :key="index">
					<view class="con_1_top c-333" @click="selectAddress(item)">
						<view class="display p_bottom_10 fw-b">
							<view class="p_right_10">
								{{item.name}}
							</view>
							<view class="p_right_10">
								{{item.gender==1?"先生":"女士"}}
							</view>
							<view class="">
								{{item.mobile}}
							</view>
						</view>
						<view class="c-666">
							{{item.addresss}}
						</view>
					</view>
					<view class="display con_1_bottom">
						<view class="display">
							<radio @click="radioChange(item)" :checked="item.is_default==1?true:false" color="#f38613" style="transform:scale(0.7)"></radio>
							<view class="c-999">
								设为默认地址
							</view>
						</view>
						<view class="display c-999">
							<view class="display" @click="bianji(item)">
								<u-icon name="zhuanfa" color="#999999" size="35"></u-icon>
								<view style="padding-left: 5px;">编辑</view>
							</view>
							<view class="display" style="padding-left: 20px;" @click="shanchu(item)">
								<u-icon name="minus-circle" color="#999999" size="35"></u-icon>
								<view style="padding-left: 5px;">删除</view>
							</view>
						</view>
					</view>
				</view>
			</radio-group>
		
			<view class="addBtn" @click="Add_address">
				添加新地址
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background:{backgroundColor: '#ffffff',}, //导航栏背景
				addressList:[], //地址信息
				
				value1:1, //默认地址
				type:1, // 0/1  订单选择地址/收货地址
				
			}
		},
		onLoad(e) {
			console.log(e);
			if(e.type){
				this.type=e.type
			}
			this.getAddress() //获取地址列表
			uni.$on('editAddress',res=>{
				this.getAddress() //刷新地址列表
			})
		},
		methods: {
			getAddress(){ //获取收货地址列表
				this.$myRequest('address/index',{})
				.then(res=>{
					console.log(res.data);
					this.addressList=res.data.data.list
					this.addressList.forEach(i=>{
						i.addresss=i.province_name+i.city_name+i.area_name+i.address
					})
				})
			},
			radioChange(item){ //选择默认地址
				this.$myRequest('address/default_edit',{
					address_id:item.id
				}).then(res=>{
					console.log(res.data);
					this.$all.toast(res.data.msg)
					if(res.data.code==1){
						// this.getAddress() //刷新地址列表
					}
				})
			},
			selectAddress(item){ //选择地址
				if(this.type==0){ //如果从订单详情进入
					uni.$emit('address',item)
					uni.navigateBack()
				}
				// console.log(item);
			},
			bianji(item){ //编辑地址
				console.log(item);
				uni.navigateTo({ 
					url:"editAddress?address="+ encodeURIComponent(JSON.stringify(item))
				})
			},
			shanchu(item){ //删除地址
				this.$myRequest('address/delAddress',{
					id:item.id
				}).then(res=>{
					console.log(res.data);
					this.$all.toast(res.data.msg)
					if(res.data.code==1){
						this.getAddress() //刷新地址列表
					}
				})
				
			},
			Add_address(){ //新增地址
				uni.navigateTo({
					url:"editAddress?type="+0
				})
			},
			
		}
	}
</script>

<style lang="scss">
page{
	background-color: #fafafa;
}
.content{
	padding: 10px 0;
	.con_1{
		padding: 20px;background-color: #FFFFFF;margin-bottom: 10px;width: 750rpx;
		.con_1_top{
			border-bottom: 1px solid #eeeeee;padding-bottom: 15px;justify-content: space-between;
		}
		.con_1_bottom{
			align-items: center;
			padding-top: 15px;justify-content: space-between;
			image{
				width: 14px;height: 14px;margin-right: 10px;
			}
		}
	}
	.addBtn{
		color: #FFFFFF;border-radius: 30px;margin: 20px;padding: 15px 0;text-align: center;
		background: linear-gradient(87deg, #F3A613 0%, #F38313 100%);
	}

}


</style>
